<?php
namespace App\Http\Controllers\Utils;

use App\Admin\Eloquent\UserInfo;
use App\Eloquent\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

/**
 * 辅助工具
 *
 * Class UtilsController
 * @package App\Http\Controllers\Controller\Utils
 */
class UtilsController extends Controller
{
    /**
     * UtilsController constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取图片验证码
     */
    public function captcha()
    {
        return redirect(captcha_src('flat'));
    }

    /**
     * 校验图片验证码是否正确
     * @param Request $request
     * @return string
     */
    public function captcha_check(Request $request)
    {
        //获取session中的验证码key值,该值在创建验证码时已经自动生成并保存session
        $key = $request->session()->get('captcha.key');

        $value = $request->input('captcha');
        $value = strtolower($value);

        if (password_verify($value, $key)) {
            return 'true';
        } else {
            return 'false';
        }
    }

    public function emailvalidate($id, $code)
    {
        return 'id : ' . $id . '     code: ' . $code;
    }

    /**
     * 查询用户名是否存在
     *
     * @param Request $request
     * @return string
     */
    public function userNameExists(Request $request)
    {
        $username = $request->only('username');


        if (User::where('username', $username)->exists()) {
            return 'false';
        } else {
            return 'true';
        }
    }

    /**
     * 查询用户邮箱是否存在
     *
     * @param Request $request
     * @return string
     */
    public function userEmailExists(Request $request)
    {
        $email = $request->only('email');

        if (User::where('email', $email)->exists()) {
            return 'false';
        } else {
            return 'true';
        }
    }

    /**
     * 查询手机号码是否存在
     *
     * @param Request $request
     * @return string
     */
    public function userPhoneNumberExists(Request $request)
    {
        $phone_number = $request->input('phone_number');

        if (empty($phone_number)) {
            $phone_number = $request->only('telephone');
        }

        if (User::where('phone_number', $phone_number)->exists()) {
            return 'false';
        } else {
            return 'true';
        }
    }

    

    /**
     * 随机生成推荐码
     *
     * @return mixed
     */
    public static function createInviteCode()
    {
        do {
            //为用户生成邀请码
            $code = create_code(6);

        } while (UserInfo::where('invite_code', $code)->exists());

        return $code;
    }

    /**
     * 随机生成sina支付用户名
     *
     * @return mixed
     */
    public static function createIdentityID()
    {
        do {
            //为用户生成sina用户ID
            $code = 'bbl' . create_code(6);

        } while (User::where('identity_id', '=', $code)->exists());

        return $code;
    }
    /**
     * 生成企业会员ID
     * 
     * @return string
     */
    public static function createAuditMemberID(){
        do{
            //为用户生成企业会员ID
            $code = 'ebbl' . create_code(6);

        }while(User::where('identity_id', '=', $code)->exists());

        return $code;
    }
    public function bindEmail(string $v_email,string $time){

        //找到缓存中的待验证的邮箱
        if(!$data = Cache::pull($v_email)){
            return abort(404);
        }

        //对比 link生成时间
        if($data['time'] != $time){ 
            return abort(404);
        }

        //验证通过
        //为用户绑定邮箱
        $user = User::findOrFail($data['uid']);
        $user->email = $data['email'];
        $user->email_verified = 1;
        $user->save();
        
        return view('web.user_center.email_validator');
    }


    /**
     * 去一个过渡页面
     * 
     * @return [type] [description]
     */
    public function transition(){
        return view('web.auth.transition');
    }
}